package UPPM;

import java.util.ResourceBundle;
import java.util.concurrent.TimeUnit;

import junit.framework.Assert;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

import UPPM.Utils;

public class TestLogin
{
	WebDriver driver = new FirefoxDriver();

	@Before
	public void setUp() throws Exception {
		driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
		driver.manage().window().maximize();
		driver.get("http://test.unippmtest.co.uk");
		}

	@Test
	public void testLoginUiInitialState() throws Exception {

		Thread.sleep(1000);
		driver.findElement(By.id("login")).click();
//		Thread.sleep(2000);	
//		Thread.sleep(5000);
		Utils.waitForElement(driver, By.id("login_overlay"));
		
		WebElement user = driver.findElement(By.id("loginUser"));
		Thread.sleep(1000);
		System.out.println("user " + user.getText());
		WebElement labelUser = driver.findElement(By.cssSelector("#user label"));
		String userLabelText = labelUser.getText().trim();
		Thread.sleep(5000);
//		Assert.assertEquals("Username:", driver.findElement(By.cssSelector("#user label")).getText().trim());
		
		
		WebElement password = driver.findElement(By.id("loginPwd"));
		String passText = password.getText().trim();
//		Assert.assertEquals("Password:", passText);
		
		
		WebElement rememberMeCkbx = driver.findElement(By.id("rememberme"));
		Assert.assertEquals(true, rememberMeCkbx.isEnabled());
		
		WebElement rememberMe = driver.findElement(By.id("remembermelabel"));
		Assert.assertEquals( "remember me", rememberMe.getText().trim());
		
		WebElement forgotPwd = driver.findElement(By.id("forgotPwd"));
		Assert.assertEquals( "Forgot password", forgotPwd.getText().trim());
		
		WebElement loginBtn = driver.findElement(By.id("loginButton"));
		Assert.assertEquals(true, loginBtn.isEnabled());
				
	}

	//This test validates login with wrong user/pwd
	@Test
	public void testLoginNegativeInvalidUserAndPassword() throws Exception {
				
		Thread.sleep(1000);
		driver.findElement(By.id("login")).click();
		Utils.waitForElement(driver, By.id("login_overlay"));
		
		WebElement user = driver.findElement(By.id("loginUser"));
		user.sendKeys("test"); 
		Thread.sleep(5000);
		WebElement password = driver.findElement(By.id("loginPwd"));
		password.sendKeys("password");
		Thread.sleep(500);
		WebElement loginBtn = driver.findElement(By.id("loginButton"));
		loginBtn.click();
		Thread.sleep(500);
		WebElement loginErr = driver.findElement(By.id("loginErrorUl"));
		Assert.assertEquals(loginErr.isDisplayed(), true);
		
				
	}
	
	//This test validates login with valid user/pwd
	@Test
	public void testLoginPositive() throws Exception {
		
		//Utils.acceptCookies(driver); 
		Utils.Login(driver);
		Utils.waitForElement(driver, By.id("yourDownloads"));

	}
	
	
	@Test
	public void testLoginNegativeInvalidPassword() throws Exception {
		
		Thread.sleep(1000);
		driver.findElement(By.id("login")).click();
		Utils.waitForElement(driver, By.id("login_overlay"));
		
		WebElement user = driver.findElement(By.id("loginUser"));
		user.sendKeys("example@example.com");
		
		WebElement password = driver.findElement(By.id("loginPwd"));
		password.sendKeys("wrongpassword");
				
		WebElement loginBtn = driver.findElement(By.id("loginButton"));
		loginBtn.click();
		
//		Thread.sleep(1000L); 
		Assert.assertEquals(true, Utils.isElementPresent(driver, By.id("loginErrorDiv"))); 
		
	}
	
	@After
	public void tearDown() throws Exception {
		System.out.println("******************************");
		driver.quit();
	}
}
